package com.it.androidCommonUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;


/**
 * 日期和时间的常用类<br>
 * 方法都是static的,可通过类名调用<br>
 */
public class DateTimeUtil {

	private static final String DATE_PATTERN = "yyyy-MM-dd";
	private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	private static final String[] PARSEPATTERNS = new String[] {
			"yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss",
			"yyyy.MM.dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd HH:mm",
			"yyyy.MM.dd HH:mm", "yyyy-MM-dd HH", "yyyy/MM/dd HH",
			"yyyy.MM.dd HH", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy.MM.dd" };
	
	private static final String pattern_yyyyMMdd="yyyyMMdd";
	

	

	// 不允许实例化该类
	private DateTimeUtil() {
	}

	/**
	 * 获得系统当前日期时间
	 * 
	 * @return 格式为 yyyy-MM-dd HH:mm:ss
	 */
	public static String getNowDateTime() {
		return new SimpleDateFormat(DATE_TIME_PATTERN).format(new Date());
	}

	/**
	 * 获得系统当前日期
	 * 
	 * @return 格式为 yyyy-MM-dd
	 */
	public static String getDate() {
		return (new SimpleDateFormat(DATE_PATTERN)).format(new Date());
	}

	/**
	 * 获得指定时间的标准返回格式
	 * 
	 * @param year
	 *            指定的年份
	 * @param month
	 *            指定的月份
	 * @param day
	 *            指定的日期
	 * @return 格式为 yyyy-MM-dd
	 */
	public static String getDate(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		return (new SimpleDateFormat(DATE_PATTERN)).format(calendar.getTime());
	}

	/**
	 * 根据起始日期、周次、星期获得具体的日期
	 * 
	 * @param firstDate
	 *            起始日期
	 * @param zc
	 *            第几周
	 * @param xq
	 *            星期几
	 * @return
	 */
	public static String getDateByWeek(String firstDate, int zc, int xq) {
		String result = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance(Locale.CHINA);
			cal.setTime(sdf.parse(firstDate));
			int firstWeek = cal.get(Calendar.WEEK_OF_YEAR);// 获得起始的周次
			cal.set(Calendar.WEEK_OF_YEAR, (firstWeek + zc) - 1);
			cal.set(Calendar.DAY_OF_WEEK, getXq(xq));
			result = sdf.format(cal.getTime());
		} catch (Exception e) {

		}
		return result;
	}

	/**
	 * 中国的星期与外国星期转换
	 * 
	 * @param xq
	 * @return
	 */
	public static int getXq(int xq) {
		int result = 0;
		if (xq == 1) {
			result = Calendar.MONDAY;// 中国星期一对应国外星期二
		} else if (xq == 2) {
			result = Calendar.TUESDAY;// 星期二
		} else if (xq == 3) {
			result = Calendar.WEDNESDAY;// 星期三
		} else if (xq == 4) {
			result = Calendar.THURSDAY;// 星期四
		} else if (xq == 5) {
			result = Calendar.FRIDAY;// 星期五
		} else if (xq == 6) {
			result = Calendar.SATURDAY;// 星期六
		} else if (xq == 7) {
			result = Calendar.SUNDAY;// 星期日
		}
		return result;
	}

	/**
	 * 得到"yyyy年M月d日"格式的日期
	 * 
	 * @return "yyyy年M月d日"格式的日期
	 */
	public static String getChineseDate() {
		return (new SimpleDateFormat("yyyy\u5E74M\u6708d\u65E5"))
				.format(new Date());
	}

	/**
	 * 得到当前的时间,格式为"HH:mm:ss"
	 * 
	 * @return 当前时间,格式为"HH:mm:ss"
	 */
	public static String getTime() {
		return (new SimpleDateFormat("HH:mm:ss")).format(new Date());
	}

	/**
	 * 得到当前时间
	 * 
	 * @return 数组格式的当前时间,array[0]为小时,array[1]为分钟 String[]
	 */
	public static String[] getTimeForHourMinute() {
		String time[] = new String[2];
		String timeStr = (new SimpleDateFormat("HH:mm:ss")).format(new Date());
		String temp[] = timeStr.split(":");
		time[0] = temp[0];
		time[1] = temp[1];
		return time;
	}

	/**
	 * 得到当前日期的星期
	 * 
	 * @return 当前日期的星期 String
	 */
	public static String getWeekday() {
		return (new SimpleDateFormat("E")).format(new Date());
	}

	/**
	 * 得到指定日期的星期
	 * 
	 * @param year
	 *            指定的年份
	 * @param month
	 *            指定的月份
	 * @param day
	 *            指定的日期
	 * @return 日期 String
	 */
	public static String getWeekday(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		return (new SimpleDateFormat("E")).format(calendar.getTime());
	}

	/**
	 * 得到当前日期的年份和月份
	 * 
	 * @return 以"yyyy-MM"表示的年份和月份 String
	 */
	public static String getYearAndMonth() {
		return (new SimpleDateFormat("yyyy-MM")).format(new Date());
	}

	/**
	 * 得到指定日期的年份和月份
	 * 
	 * @param year
	 *            指定的年份
	 * @param month
	 *            指定的月份
	 * @param day
	 *            指定的日期
	 * @return 年份和月份 String
	 */
	public static String getYearAndMonth(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		return (new SimpleDateFormat("yyyy-MM")).format(calendar.getTime());
	}

	/**
	 * 得到当前日期是一年中的第几天
	 * 
	 * @return String
	 */
	public static String getDateInYear() {
		return (new SimpleDateFormat("DDD")).format(new Date());
	}

	/**
	 * 得到指定日期是一年中的第几天
	 * 
	 * @param year
	 *            指定的年份
	 * @param month
	 *            指定的月份
	 * @param day
	 *            指定的日期
	 * @return 指定日期在一年中的第几天 String
	 */
	public static String getDateInYear(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		return (new SimpleDateFormat("DDD")).format(calendar.getTime());
	}

	/**
	 * 得到当前日期是一年中的第几个星期
	 * 
	 * @return String
	 */
	public static String getWeekInYear() {
		return (new SimpleDateFormat("ww")).format(new Date());
	}

	/**
	 * 得到指定日期是一年中的第几个星期
	 * 
	 * @param year
	 *            指定的年份
	 * @param month
	 *            指定的月份
	 * @param day
	 *            指定的日期
	 * @return 指定日期是一年中的第几个星期 String
	 */
	public static String getWeekInYear(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		return (new SimpleDateFormat("ww")).format(calendar.getTime());
	}

	/**
	 * 得到指定日期是当前月的第几个星期
	 * 
	 * @return String
	 */
	public static String getWeekInMonth() {
		return (new SimpleDateFormat("WW")).format(new Date());
	}

	/**
	 * 得到指定日期是所在月份的第几个星期
	 * 
	 * @param year
	 *            指定的年份
	 * @param month
	 *            指定的月份
	 * @param day
	 *            指定的日期
	 * @return 指定日期是所在月份的第几个星期 String
	 */
	public static String getWeekInMonth(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		return (new SimpleDateFormat("WW")).format(calendar.getTime());
	}

	/**
	 * 得到当前日期前beforeNum天的日期
	 * 
	 * @param beforeNum
	 *            提前量
	 * @return String
	 */
	public static String getDateByBefore(int beforeNum) {
		Calendar now = Calendar.getInstance();
		now.add(6, -1 * beforeNum);
		return (new SimpleDateFormat("yyyy-MM-dd")).format(now.getTime());
	}

	/**
	 * 得到指定日期前beforeNum天的日期
	 * 
	 * @param year
	 *            指定的年份
	 * @param month
	 *            指定的月份
	 * @param day
	 *            指定的日期
	 * @param beforeNum
	 *            提前量
	 * @return String
	 */
	public static String getDateByBefore(int year, int month, int day,
			int beforeNum) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		calendar.add(6, -1 * beforeNum);
		return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
	}

	/**
	 * 得到指定日期前beforeNum天的日期
	 * 
	 * @param dateString
	 *            以"yyyy-MM-dd"格式指定的日期
	 * @param beforeNum
	 *            提前量
	 * @return String
	 */
	public static String getDateByBefore(String dateString, int beforeNum) {
		Calendar calendar = Calendar.getInstance();
		try {
			Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateString);
			calendar.setTime(date);
		} catch (Exception e) {
			return "";
		}
		calendar.add(6, -1 * beforeNum);
		return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
	}

	/**
	 * 得到指定日期后afterNum天的日期
	 * 
	 * @param dateString
	 *            以"yyyy-MM-dd"格式指定的日期
	 * @param afterNum
	 *            偏移量
	 * @return String
	 */
	public static String getDateByAfter(String dateString, int afterNum) {
		Calendar calendar = Calendar.getInstance();
		try {
			Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateString);
			calendar.setTime(date);
		} catch (Exception e) {
			return "";
		}
		calendar.add(6, afterNum);
		return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
	}

	public static String getWeekdayByBefore(int beforeNum) {
		Calendar now = Calendar.getInstance();
		now.add(6, -1 * beforeNum);
		return (new SimpleDateFormat("E")).format(now.getTime());
	}

	public static String getWeekdayByBefore(int year, int month, int day,
			int beforeNum) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		calendar.add(6, -1 * beforeNum);
		return (new SimpleDateFormat("E")).format(calendar.getTime());
	}

	public static String getDateByAfter(int afterNum) {
		Calendar now = Calendar.getInstance();
		now.add(6, afterNum);
		return (new SimpleDateFormat("yyyy-MM-dd")).format(now.getTime());
	}

	public static String getDateByAfter(int year, int month, int day,
			int afterNum) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		calendar.add(6, afterNum);
		return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
	}

	public static String getWeekdayByAfter(int afterNum) {
		Calendar now = Calendar.getInstance();
		now.add(6, afterNum);
		return (new SimpleDateFormat("E")).format(now.getTime());
	}

	public static String getWeekdayByAfter(int year, int month, int day,
			int afterNum) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		calendar.add(6, afterNum);
		return (new SimpleDateFormat("E")).format(calendar.getTime());
	}

	public static String getDateOfWeekend() {
		Calendar now = Calendar.getInstance();
		now.add(6, 7 - now.get(7));
		return (new SimpleDateFormat("yyyy-MM-dd")).format(now.getTime());
	}

	public static String getDateOfWeekend(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		calendar.add(6, 7 - calendar.get(7));
		return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
	}

	public static String getDateOfWeekstart() {
		Calendar now = Calendar.getInstance();
		now.add(6, 1 - now.get(7));
		return (new SimpleDateFormat("yyyy-MM-dd")).format(now.getTime());
	}

	public static String getDateOfWeekstart(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		calendar.add(6, 1 - calendar.get(7));
		return (new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getTime());
	}

	public static int getDateOfMonthend() {
		Calendar now = Calendar.getInstance();
		return now.getActualMaximum(5);
	}

	public static int getDateOfMonthend(int year, int month, int day) {
		GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
		return calendar.getActualMaximum(5);
	}

	public static String getDateBefore(String timeString, int minute) {
		long min = 0L;
		try {
			Date date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
					.parse(timeString);
			min = date.getTime();
		} catch (Exception exception) {
		}
		return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(
				min - (minute * 60 * 1000)));
	}

	public static boolean IsOverTime(String timeString, int rating) {
		try {
			Date date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
					.parse(timeString);
			Date now = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
					.parse((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
							.format(new Date()));
			long min = date.getTime();
			long nowmin = now.getTime();
			return nowmin - min > (rating * 60 * 1000);
		} catch (Exception e) {
			return false;
		}
	}

	public static String switchDateFormat(String dateStr) {
		Date date = null;
		try {
			date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateStr);
		} catch (Exception exception) {
		}
		return (new SimpleDateFormat("yyyy\u5E74M\u6708d\u65E5")).format(date);
	}

	public static String switchDateFormat(String firstFormat,
			String secondFormat, String dateStr) {
		Date date = null;
		try {
			date = (new SimpleDateFormat(firstFormat)).parse(dateStr);
		} catch (Exception exception) {
		}
		return (new SimpleDateFormat(secondFormat)).format(date);
	}

	public static String getWeekdayByDateString(String dateString) {
		String weekday = "";
		try {
			Date date = (new SimpleDateFormat("yyyy-MM-dd")).parse(dateString);
			weekday = (new SimpleDateFormat("E")).format(date);
		} catch (Exception exception) {
		}
		return weekday;
	}

	public static String getWeekdayByDate(Date date) {
		return (new SimpleDateFormat("E")).format(date);
	}

	public static String getDateTimeZone() {
		return (new SimpleDateFormat("yyyyMMddHHmmssS")).format(new Date());
	}

	public static String getYear() {
		return (new SimpleDateFormat("yyyy")).format(new Date());
	}

	/**
	 * 根据传入yyyy-MM-dd HH:mm:ss格式的日期,获得季度<br>
	 * 返回String数组 分别存放当前季度的开始时间,结束时间,当前季度,当前季度的中文形式<br>
	 * 例如:
	 * 
	 * <pre>
	 *  2008-10-01
	 *  2008-12-31
	 *  4
	 *  2008年第4季度
	 * </pre>
	 * 
	 * @param date
	 *            yyyy-MM-dd HH:mm:ss格式的日期
	 * @return
	 */
	public static String[] getQuarter(Date date) {
		String record[] = new String[4];
		String startDateStr = "";
		String endDateStr = "";
		String quarter = null;
		String year = (new SimpleDateFormat("yyyy")).format(date);
		String month = (new SimpleDateFormat("MM")).format(date);
		int monthInteger = Integer.parseInt(month);
		if (monthInteger >= 1 && monthInteger <= 3) {
			startDateStr = "-01-01";
			endDateStr = "-03-31";
			quarter = "1";
		}
		if (monthInteger >= 4 && monthInteger <= 6) {
			startDateStr = "-04-01";
			endDateStr = "-06-30";
			quarter = "2";
		}
		if (monthInteger >= 7 && monthInteger <= 9) {
			startDateStr = "-07-01";
			endDateStr = "-09-30";
			quarter = "3";
		}
		if (monthInteger >= 9 && monthInteger <= 12) {
			startDateStr = "-10-01";
			endDateStr = "-12-31";
			quarter = "4";
		}
		record[0] = year + startDateStr;
		record[1] = year + endDateStr;
		record[2] = quarter;
		record[3] = year + "\u5E74\u7B2C" + quarter + "\u5B63\u5EA6";
		return record;
	}

	/**
	 * 获得当前季度的相关信息<br>
	 * 返回String数组 分别存放当前季度的开始时间,结束时间,当前季度,当前季度的中文形式<br>
	 * 例如:
	 * 
	 * <pre>
	 *  2008-10-01
	 *  2008-12-31
	 *  4
	 *  2008年第4季度
	 * </pre>
	 * 
	 * @return
	 */
	public static String[] getNowQuarter() {
		String record[] = new String[4];
		String startDateStr = "";
		String endDateStr = "";
		String quarter = "";
		Date date = new Date();
		String year = (new SimpleDateFormat("yyyy")).format(date);
		String month = (new SimpleDateFormat("MM")).format(date);
		int monthInteger = Integer.parseInt(month);
		if (monthInteger >= 1 && monthInteger <= 3) {
			startDateStr = "-01-01";
			endDateStr = "-03-31";
			quarter = "1";
		}
		if (monthInteger >= 4 && monthInteger <= 6) {
			startDateStr = "-04-01";
			endDateStr = "-06-30";
			quarter = "2";
		}
		if (monthInteger >= 7 && monthInteger <= 9) {
			startDateStr = "-07-01";
			endDateStr = "-09-30";
			quarter = "3";
		}
		if (monthInteger >= 9 && monthInteger <= 12) {
			startDateStr = "-10-01";
			endDateStr = "-12-31";
			quarter = "4";
		}
		record[0] = year + startDateStr;
		record[1] = year + endDateStr;
		record[2] = quarter;
		record[3] = year + "\u5E74\u7B2C" + quarter + "\u5B63\u5EA6";
		return record;
	}

	public static String[] getDownQuarter(String date, String quarter)
			throws Exception {
		String record[] = new String[4];
		String startDateStr = "";
		String endDateStr = "";
		int monthInteger = Integer.parseInt(quarter);
		String year = (new SimpleDateFormat("yyyy"))
				.format((new SimpleDateFormat("yyyy-MM-dd")).parse(date));
		int yearInteger = Integer.parseInt(year);
		if (monthInteger == 1) {
			startDateStr = year + "-04-01";
			endDateStr = year + "-06-30";
			quarter = "2";
		}
		if (monthInteger == 2) {
			startDateStr = year + "-07-01";
			endDateStr = year + "-09-30";
			quarter = "3";
		}
		if (monthInteger == 3) {
			startDateStr = year + "-10-01";
			endDateStr = year + "-12-31";
			quarter = "4";
		}
		if (monthInteger == 4) {
			startDateStr = (yearInteger + 1) + "-01-01";
			endDateStr = (yearInteger + 1) + "-03-31";
			quarter = "1";
			year = Integer.toString(yearInteger + 1);
		}
		record[0] = startDateStr;
		record[1] = endDateStr;
		record[2] = quarter;
		record[3] = year + "\u5E74\u7B2C" + quarter + "\u5B63\u5EA6";
		return record;
	}

	public static String[] getUpQuarter(String date, String quarter)
			throws Exception {
		String record[] = new String[4];
		String startDateStr = "";
		String endDateStr = "";
		int monthInteger = Integer.parseInt(quarter);
		String year = (new SimpleDateFormat("yyyy"))
				.format((new SimpleDateFormat("yyyy-MM-dd")).parse(date));
		int yearInteger = Integer.parseInt(year);
		if (monthInteger == 1) {
			startDateStr = (yearInteger - 1) + "-10-01";
			endDateStr = (yearInteger - 1) + "-12-31";
			quarter = "4";
			year = Integer.toString(yearInteger - 1);
		}
		if (monthInteger == 2) {
			startDateStr = year + "-01-01";
			endDateStr = year + "-03-31";
			quarter = "1";
		}
		if (monthInteger == 3) {
			startDateStr = year + "-04-01";
			endDateStr = year + "-06-30";
			quarter = "2";
		}
		if (monthInteger == 4) {
			startDateStr = year + "-07-01";
			endDateStr = year + "-09-30";
			quarter = "3";
		}
		record[0] = startDateStr;
		record[1] = endDateStr;
		record[2] = quarter;
		record[3] = year + "\u5E74\u7B2C" + quarter + "\u5B63\u5EA6";
		return record;
	}

	public static int getExamTime(String firsttime, String secondtime, int m) {
		long record = 0L;
		try {
			Date first = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
					.parse(firsttime);
			Date second = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
					.parse(secondtime);
			Date now = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
					.parse((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
							.format(new Date()));
			long m_first = first.getTime();
			long m_second = second.getTime();
			if (m_first == m_second) {
				record = m * 60 * 1000;
			} else {
				record = (m * 60 * 1000) - (now.getTime() - first.getTime());
			}
		} catch (Exception exception) {
		}
		return (int) record / 60000;
	}

	public static String switchDateStr(String date) {
		String record = "";
		try {
			Date first = (new SimpleDateFormat("yyyy-MM-dd HH")).parse(date);
			record = (new SimpleDateFormat("yyyyMMddHH")).format(first);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return record;
	}

	public static String switchDateStrForWorkList(String date) {
		String record = "";
		try {
			Date first = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"))
					.parse(date);
			record = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
					.format(first);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return record;
	}

	public static String uniteDateTime(String date, String time) {
		return date + " " + time;
	}

	public static String getOffsetTime(String format, int field, int offset) {
		Calendar calendar = Calendar.getInstance();

		calendar.add(field, offset);
		return new SimpleDateFormat(format).format(calendar.getTime());
	}

	public static String getOffsetTime(String timeString, String format,
			int field, int offset) {
		Calendar calendar = Calendar.getInstance();

		calendar.add(field, offset);
		return new SimpleDateFormat(format).format(calendar.getTime());
	}

	public static String getDatePattern() {
		return DATE_PATTERN;
	}

	public static final String getDate(Date date) {
		SimpleDateFormat df = null;
		String returnValue = "";

		if (date != null) {
			df = new SimpleDateFormat(DATE_PATTERN);
			returnValue = df.format(date);
		}

		return (returnValue);
	}

	public static final Date convertStringToDate(String pattern, String strDate)
			throws ParseException {
		SimpleDateFormat df = null;
		Date date = null;
		df = new SimpleDateFormat(pattern);

		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			// log.error("ParseException: " + pe);
			throw new ParseException(pe.getMessage(), pe.getErrorOffset());
		}

		return (date);
	}

	/**
	 * 把传入的字符串转换为Date
	 * 
	 * @param aMask
	 *            传入的字符串的格式
	 * @param strDate
	 *            时间日期字符串
	 * @return
	 * @throws ParseException
	 */
	public static final Date convertStringToDateTime(String pattern,
			String strDate) throws ParseException {
		SimpleDateFormat df = null;
		Date date = null;
		df = new SimpleDateFormat(pattern);

		try {
			date = df.parse(strDate);
		} catch (ParseException pe) {
			// log.error("ParseException: " + pe);
			throw new ParseException(pe.getMessage(), pe.getErrorOffset());
		}

		return (date);
	}


	/**
	 * 将传入的字符串转换为Date类型
	 * 
	 * @param strDate
	 *            需要转换的字符串,格式为'yyyy-MM-dd HH:mm:ss'
	 * @return
	 * @throws ParseException
	 */
	public static Date convertStringToDateTime(String strDate)
			throws ParseException {
		Date aDate = null;

		try {
			/*
			 * if (log.isDebugEnabled()) {
			 * log.debug("converting date with pattern: " + DATETIMEPATTERN); }
			 */

			aDate = convertStringToDate(DATE_TIME_PATTERN, strDate);
		} catch (ParseException pe) {
			// log.error("Could not convert '" + strDate +
			// "' to a date, throwing exception");
			pe.printStackTrace();
			throw new ParseException(pe.getMessage(), pe.getErrorOffset());

		}

		return aDate;
	}


	/**
	 * 得到 当前系统时间一周内的时间 日期转字符串 yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String getWeekBeforeNow() {

		Calendar c = Calendar.getInstance();// 此时打印它获取的是系统当前时间
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String nowDate = sdf.format(date);
		String dayBefore = "";
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 6);
		Date beforeDate = c.getTime();
		dayBefore = sdf.format(beforeDate);
		return dayBefore;

	}
	
	

  

    /**
     * 根据日期获取时间戳
     * @param mDate 日期格式为yyyy-MM-dd hh:mm:ss
     * @return
     */
    public static long getTime(String mDate){
        if (isDate(mDate,DATE_TIME_PATTERN)){
            SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_PATTERN);
            String dateString = mDate;
            try {
                Date date  = df.parse(dateString);
                long millisTime = date.getTime();
                return  millisTime;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    /**
     * 获得 YY-MM-DD
     * @param offset 0==当天  1往后加一天  -1往前减一天
     * @return
     */
    public static String getYMD(int offset){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,offset);
        int year = calendar.get(Calendar.YEAR);//获取年份
        int month = calendar.get(Calendar.MONTH)+1;//获取月份
        int day = calendar.get(Calendar.DATE);//获取日
        String temp = year+"";
        if(month < 10){
            temp += "-0"+month;
        }else {
            temp += "-"+month;
        }
        if (day < 10){
            temp += "-0"+day;
        }else {
            temp += "-"+day;
        }
        return temp;
    }

   

    /**
     * 根据日期 获得 YY-MM-DD
     * @param offset 0==传过来日期  1往后加一天  -1往前减一天
     * @return
     */
    public static String getYMD(String date, int offset){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        if(isDate(date, "yyyy-MM-dd")){
            try {
                calendar.setTime(dateFormat.parse(date));
                calendar.add(Calendar.DAY_OF_MONTH,offset);
                int year = calendar.get(Calendar.YEAR);//获取年份
                int month = calendar.get(Calendar.MONTH)+1;//获取月份
                int day = calendar.get(Calendar.DATE);//获取日
                String temp = year+"";
                if(month < 10){
                    temp += "-0"+month;
                }else {
                    temp += "-"+month;
                }
                if (day < 10){
                    temp += "-0"+day;
                }else {
                    temp += "-"+day;
                }
                return temp;
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return "";
    }

  
    /**
     * 获得 week
     * @param offset 0==当天  1往后加一天  -1往前减一天
     * @return
     */
    public static String getWeek(int offset){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,offset);
        int weekOfYear = calendar.get(Calendar.DAY_OF_WEEK);
        String week = "";
        switch (weekOfYear){
            case Calendar.MONDAY:
                week = "星期一";
                break;
            case  Calendar.TUESDAY:
                week = "星期二";
                break;
            case Calendar.WEDNESDAY:
                week = "星期三";
                break;
            case Calendar.THURSDAY:
                week = "星期四";
                break;
            case Calendar.FRIDAY:
                week = "星期五";
                break;
            case Calendar.SATURDAY:
                week = "星期六";
                break;
            case Calendar.SUNDAY:
                week = "星期日";
                break;
        }
        return week;
    }


   
    /**
     * 判断是否为合法的日期时间字符串
     * @param str_input
     * @param str_input
     * @return boolean;符合为true,不符合为false
     */
    public static  boolean isDate(String str_input,String rDateFormat){
        if (!isNull(str_input)) {
            SimpleDateFormat formatter = new SimpleDateFormat(rDateFormat);
            formatter.setLenient(false);
            try {
                formatter.format(formatter.parse(str_input));
            } catch (Exception e) {
                return false;
            }
            return true;
        }
        return false;
    }

    public static boolean isNull(String str){
        if(str==null)
            return true;
        else
            return false;
    }
	
    
    
    
	
	
}